<?php
/**
 * @title 47. 全排列 II - 递归
 * @author start2004
 */


class Solution {

    /**
     * @param Integer[] $nums
     * @return Integer[][]
     */
    function permuteUnique($nums) {
        /**
         * @since 2020-09-08 只有一个元素
         */
        if(count($nums) == 1){
            return [$nums];
        } else {}

        /**
         * @since 2020-09-08 结果集
         */
        $result = [];

        /**
         * @since 2020-09-08 依次做最后一个节点
         */
        $hashs = [];
        foreach ($nums as $key=>$num){
            /**
             * @since 2020-09-08 防止重复
             */
            if(isset($hashs[$num])){
                continue;
            } else {
                $hashs[$num] = 1;
            }

            /**
             * @since 2020-09-08 剔除这个元素, 剩下的元素再全排列
             */
            $next = $nums;
            unset($next[$key]);
            $list = self::permuteUnique($next);

            /**
             * @since 2020-09-08 添加元素num
             */
            foreach ($list as $d){
                $d[] = $num;
                $result[] = $d;
            }
        }

        /**
         * @return
         */
        return $result;
    }
}



/**
 * @url http://127.0.0.1/leetcode/202007/2020.07.21_10.php
 */
$datas = [
    // [1,1,2],
    [3,0,3],
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    var_dump($data);

    $obj = new Solution();
    $result = $obj->permuteUnique($data);
    var_dump($result);
    echo str_repeat("<br>", 3);
}
$xhprof->end();
